﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace DMEditor
{
    public class DM10Soundset
    {
        private byte _soundsetID = 0; // 0: stock, 1: BJ
        public byte soundsetID
        {
            get { return _soundsetID; }
        }

        public DM10Soundset(byte i)
        {
            _soundsetID = i;
        }

        public Dictionary<int, string> categories = new Dictionary<int, string>();

        // Index: Category ID
        // Key: Instrument ID
        // Value: Name of instrument
        public Dictionary<int, string>[] instruments;

        // Index 1: category
        // Index 2: instrument
        // Value: global ID (for module)
        public int[,] globalIDs;

        public Dictionary<int, string> allInsts = new Dictionary<int, string>();


        protected string _name;

        public string Name
        {
            get { return _name; }
        }


        public void Load(string filename)
        {
            XmlTextReader sourceXML = new XmlTextReader(filename);

            bool isSoundset = false;
            bool isCategory = false;
            bool isInstrument = false;

            int currentCategory = 0;
            int currentInstrument = 0;
            int currentID = 0;

            int numCategories = 0;
            int[] numCatInsts;
            int[] countCatInsts = new int[0];


            try
            {

                while (sourceXML.Read())
                {
                    if (sourceXML.NodeType == XmlNodeType.Element)
                    {
                        if (sourceXML.Name == "soundset")
                        {
                            isSoundset = true;
                            isCategory = isInstrument = false;
                        }

                        else if (sourceXML.Name == "categories")
                        {
                            numCategories = Int16.Parse(sourceXML.GetAttribute("num"));

                            //MessageBox.Show("Parsing " + numCategories + " categories");

                            instruments = new Dictionary<int, string>[numCategories];
                        }

                        else if (sourceXML.Name == "category")
                        {
                            isCategory = true;
                            isSoundset = isInstrument = false;

                            currentCategory = Int16.Parse(sourceXML.GetAttribute("id"));
                            instruments[currentCategory] = new Dictionary<int, string>();

                            for (int i = 0; i < Int16.Parse(sourceXML.GetAttribute("count")); ++i)
                            {
                                instruments[currentCategory].Add(i, "");
                            }
                        }


                        else if (sourceXML.Name == "instruments")
                        {
                            numCatInsts = new int[numCategories];
                            countCatInsts = new int[numCategories]; // Defaults to 0

                            int maxInsts = 0;

                            for (int i = 0; i < numCategories; ++i)
                            {
                                numCatInsts[i] = instruments[i].Count;

                                if (instruments[i].Count > maxInsts) maxInsts = instruments[i].Count;
                            }

                            globalIDs = new int[numCategories, maxInsts];
                        }

                        else if (sourceXML.Name == "instrument")
                        {
                            isInstrument = true;
                            isSoundset = isCategory = false;

                            currentCategory = Int16.Parse(sourceXML.GetAttribute("catid"));
                            currentInstrument = countCatInsts[currentCategory];
                            globalIDs[currentCategory, currentInstrument] = Int16.Parse(sourceXML.GetAttribute("globalid"));

                            currentID = Int16.Parse(sourceXML.GetAttribute("globalid"));


                        }
                    }

                    else if (sourceXML.NodeType == XmlNodeType.Text)
                    {
                        if (isSoundset == true)
                        {
                            _name = sourceXML.Value;
                            //MessageBox.Show("Name: " + _name);
                            isSoundset = false;
                        }

                        if (isCategory == true)
                        {
                            categories.Add(currentCategory, sourceXML.Value.Trim(new char[] { '\r', '\n', ' ', '\t' }));
                            //MessageBox.Show("New category " + categories[currentCategory] + " added with ID " + currentCategory);
                        }

                        if (isInstrument == true)
                        {
                            instruments[currentCategory][currentInstrument] = sourceXML.Value.Trim(new char[] { '\r', '\n', ' ', '\t' });
                            //MessageBox.Show("New instrument " + sourceXML.Value + " added with ID " + currentInstrument + " in category " + currentCategory);

                            countCatInsts[currentCategory]++;

                            allInsts.Add(currentID, sourceXML.Value.Trim(new char[] { '\r', '\n', ' ', '\t' }));
                            //Program.LogForm.AddLogItem("Added instrument " + sourceXML.Value.Trim(new char[] { '\r', '\n', ' ', '\t' }) + " with ID " + currentID);
                        }
                    }
                }
            }
            
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
                return;
            }
            

            finally
            {


                //MessageBox.Show("Soundset loaded!");
            }
             
        }
    }
}
